/*
 * File Name：TitleBar.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： TitleBar.java
 * Modify By：jejun
 * Modify Date：2014-6-11
 * Modify Type：Add
 */
package com.ciwong.mobilepay.view;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.text.TextUtils.TruncateAt;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.ciwong.libs.widget.MarqueeTextView;
import com.ciwong.mobilelib.R;
import com.ciwong.mobilepay.util.PayUtil;

/**
 * 标题栏
 * 
 * @author jejun
 * @version ciwong v.1.0 2014-6-11
 * @since ciwong v.1.0
 */
public class TitleBar extends RelativeLayout
{
    public static final int titleBar = 0xf1;

    public static final int goBack = 0xf2;

    public static final int title = 0xf3;

    public TitleBar(Context context)
    {
        super(context);
        init();
    }

    private void init()
    {
        setId(titleBar);
        setGravity(Gravity.CENTER_VERTICAL);
        setBackgroundDrawable(
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/title_bar_bg.9.png", TitleBar.class));
        ResourcesCompat.getDrawable(getResources(), R.drawable.title_bar_bg, null));

        DisplayMetrics dm = getResources().getDisplayMetrics();
        android.view.ViewGroup.LayoutParams vlp = new LayoutParams(
                LayoutParams.FILL_PARENT, (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, 45, dm));
        setLayoutParams(vlp);
        Button goBack = new Button(getContext());
        goBack.setId(TitleBar.goBack);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        addView(goBack);
        lp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        goBack.setLayoutParams(lp);
        Drawable press = ResourcesCompat.getDrawable(getResources(), R.mipmap.go_back_press,null);
        Drawable normal =
//                PayUtil.getDrawableFromJar(
//                "/images_mobilepay/go_back_normal.png", TitleBar.class);
        ResourcesCompat.getDrawable(getResources(),R.drawable.go_back_selector,null);
        Drawable selector = PayUtil.getDrawableSelector(new int[][]{
                {android.R.attr.state_pressed}, {0}}, new Drawable[]{press,
                normal});
        int height = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 30, dm);
        float scale = 20.0f / 48.0f;
        int width = (int) (height * scale);
        selector.setBounds(new Rect(0, 0, width, height));
        goBack.setCompoundDrawables(selector, null, null, null);
        goBack.setBackgroundDrawable(null);
        goBack.setGravity(Gravity.CENTER);
        goBack.setEllipsize(TruncateAt.END);
        goBack.setTextColor(0xffbcc8de);
        goBack.setPadding(width, 0, 0, 0); 
        goBack.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        // goBack.setMinWidth((int) TypedValue.applyDimension(
        // TypedValue.COMPLEX_UNIT_DIP, normal.getIntrinsicWidth(), dm));
        // goBack.setMinHeight((int) TypedValue.applyDimension(
        // TypedValue.COMPLEX_UNIT_DIP, normal.getIntrinsicHeight(), dm));
        goBack.setSingleLine(true);

        MarqueeTextView title = new MarqueeTextView(getContext());
        title.setId(TitleBar.title);
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        addView(title);
        title.setLayoutParams(lp);

    }

    public void setTitleBarHeight(int height)
    {
        android.view.ViewGroup.LayoutParams lp = getLayoutParams();
        lp.height = height;
        setLayoutParams(lp);
    }

    /**
     * 设置标题栏的背景
     * 
     * @param background
     *            标题栏背景
     */
    public void setTitleBarBackground(Drawable background)
    {
        setBackgroundDrawable(background);
    }
}
